import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';
import '../utils/const.dart';
import '../utils/storage.dart';

/// 全局配置
class GlobalConfigService extends GetxService {
  static GlobalConfigService get to => Get.find();


  /// 是否第一次登录
  RxBool isFirstOpen = false.obs;

  /// 是否离线状态
  RxBool isOfflineLogin = false.obs;

  /// 是否 release
  static bool get isRelease => const bool.fromEnvironment('dart.vm.product');

  /// 初始化
  Future<GlobalConfigService> init() async {
    WidgetsFlutterBinding.ensureInitialized();
    await StorageUtil.init();


    return this;
  }




  void exitSystem() async {
    StorageUtil().remove(STORAGE_USER_PROFILE_KEY);
  }

  ///用户持久化
  Future? saveProfile( userResponse) {

  }


  /// 动态申请定位权限
  void requestPermission() async {
    // 申请权限
    bool hasLocationPermission = await requestLocationPermission();
    if (hasLocationPermission) {
      print("定位权限申请通过");
    } else {
      print("定位权限申请不通过");
    }
  }

  /// 申请定位权限
  /// 授予定位权限返回true， 否则返回false
  Future<bool> requestLocationPermission() async {
    //获取当前的权限
    var status = await Permission.location.status;
    if (status == PermissionStatus.granted) {
      //已经授权
      return true;
    } else {
      //未授权则发起一次申请
      status = await Permission.location.request();
      if (status == PermissionStatus.granted) {
        return true;
      } else {
        return false;
      }
    }
  }
}
